import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gongge/pages/SplishPage.dart';
import 'package:gongge/pages/StartLanguagePage.dart';
import 'package:gongge/utils/AudioUtils.dart';
import 'package:provider/provider.dart';
import 'package:universal_platform/universal_platform.dart';
import 'config/AppConst.dart';
import 'generated/l10n.dart';
import 'livedata/LanguageProvider.dart';
import 'pages/HomePage.dart';
import 'utils/SpUtils.dart';

//8重新开始游戏
typedef CallBack = void Function(int selectIndex);

void main() async {
  debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  Provider.debugCheckInvalidValueType = null;
  _disableDebugPrint();
  WidgetsFlutterBinding.ensureInitialized();
  await SpUtils.preInit();
  await AudioUtils.preInit();
  runApp(MultiProvider(
    providers: [
      //多语言匹配
      ChangeNotifierProvider(create: (context) => LanguageProvider()),
    ],
    child: MyApp(),
  ));

  if (UniversalPlatform.isAndroid) {
    SystemUiOverlayStyle style = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,

        ///这是设置状态栏的图标和字体的颜色
        ///Brightness.light  一般都是显示为白色
        ///Brightness.dark 一般都是显示为黑色
        statusBarIconBrightness: Brightness.light);
    SystemChrome.setSystemUIOverlayStyle(style);
  }
}

void _disableDebugPrint() {
  bool debug = false;
  assert(() {
    debug = true;
    return true;
  }());
  if (!debug) {
    debugPrint = (message, {wrapWidth}) {
      //disable log print when not in debug mode
    };
  }
}

final RouteObserver<ModalRoute> routeObserver = RouteObserver<ModalRoute>();

class MyApp extends StatelessWidget {
  const MyApp();
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // ScreenUtil.init(context);
    return ScreenUtilInit(
        // 默认设计稿尺寸
        //  designSize: Size(750, 1334),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) {
          return Consumer<LanguageProvider>(
              builder: (context, currentLocale, child) {
            return MaterialApp(
              title: 'Stone Cube',
              localizationsDelegates: [
                S.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              supportedLocales: S.delegate.supportedLocales,
              navigatorObservers: [routeObserver],
              locale: _listenerLocal(currentLocale.language), //设置首要支持的语言),
              theme: ThemeData(
                primarySwatch: Colors.blue,
              ),

              home: getHome(context),
              // home: Scaffold(
              //   body: _HomePage(),
              // ),
            );
          });
        });
  }
}

Widget getHome(BuildContext context) {


  //支持web端
  if (UniversalPlatform.isIOS) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light, //更改iOS和android主题状态栏颜色
      child: Material(
        child: Scaffold(
          body: getBody(context),
        ),
      ),
    );
  } else {
    return getBody(context);
  }
}

Widget getBody(BuildContext context) {
  bool? choiceLanguage =
      SpUtils.getInstance()?.getBool(AppConst.choiceLanguage);
  if (choiceLanguage != null && choiceLanguage) {
    //已经选了语言
    int? isFisrt = SpUtils.getInstance()?.getInt2(AppConst.fisrt);
    if (isFisrt == null || isFisrt < 4) {
      return SplishPage();
    } else {
      return _HomePage();
    }
  } else {
    return StartLanguagePage();
  }
}

_listenerLocal(String? lag) {
 // print("保存语言是::${lag}");
  String? value = SpUtils.getInstance()?.getString(AppConst.language);
    if (value == null) {
      return Locale('en');
    } else {
      return Locale('$value');
    }
  
}

const SCREEN_BORDER_WIDTH = 3.0;

const BACKGROUND_COLOR = const Color(0xffefcc19);

class _HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //only Android/iOS support land mode
    bool land =
        MediaQuery.of(context).orientation == Orientation.landscape; //横屏的话
    //   return land ? PageLand() : PagePortrait();
    return HomePage();
  }
}
